package com.yunji.base.convert;

import com.yunji.base.cache.BaseCache;
import com.yunji.base.dao.IBelongDao;
import com.yunji.base.domain.BelongDO;
import com.yunji.base.dto.AgentDTO;
import com.yunji.common.model.domain.AgentDO;
import com.yunji.common.utils.ConvertDataUtil;
import com.yunji.common.utils.bean.BeanUtils;
import org.apache.commons.compress.utils.Lists;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author : peter-zhu
 * @date : 2024/12/9 16:49
 * @description : TODO
 **/
public class AgentConvert {

    private static void setByBelong(AgentDTO agentDTO, BelongDO belong) {
        if (belong != null) {
            agentDTO.setAgentScale(belong.getScaleRatio());
            agentDTO.setPlatformScale(belong.getDeductRatio());
            agentDTO.setAgentMode(belong.getAgentMode());
            agentDTO.setScaleRatio(belong.getScaleRatio());
            agentDTO.setDeductRatio(belong.getDeductRatio());
            agentDTO.setEarnAmount(belong.getEarnAmount());
            agentDTO.setCashAmount(belong.getCashAmount());
            agentDTO.setRemainAmount(belong.getRemainAmount());
            agentDTO.setFrozenAmount(belong.getFrozenAmount());
            //  b.refund_amount   AS refundAmount
        }
    }

    public static List<AgentDTO> domain2dto(List<AgentDO> agents, IBelongDao belongDao) {
        Set<Long> agentBids = ConvertDataUtil.set2FieldList(agents, AgentDO::getBelongId);
        Map<Long, BelongDO> dataMap = ConvertDataUtil.getDataMap(() -> belongDao.listByIds(agentBids), BelongDO::getBelongId);
        List<AgentDTO> dtoList = Lists.newArrayList();
        for (AgentDO agent : agents) {
            AgentDTO agentDTO = new AgentDTO();
            BeanUtils.clone(agent, agentDTO);
            BelongDO belong = dataMap.get(agent.getBelongId());
            setByBelong(agentDTO, belong);
            dtoList.add(agentDTO);
        }
        return dtoList;
    }

    public static AgentDTO domain2dto(AgentDO agent) {
        if (agent == null) {return null;}
        AgentDTO agentDTO = new AgentDTO();
        BeanUtils.clone(agent, agentDTO);
        BelongDO belong = BaseCache.getBelong(agent.getBelongId());
        setByBelong(agentDTO, belong);
        return agentDTO;
    }
}
